package com.hong.taotaomall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hong.taotaomall.common.BaseContext;
import com.hong.taotaomall.common.CustomException;
import com.hong.taotaomall.entity.Goods;
import com.hong.taotaomall.entity.Orders;
import com.hong.taotaomall.entity.Result;
import com.hong.taotaomall.entity.ShoppingCart;
import com.hong.taotaomall.service.IGoodsService;
import com.hong.taotaomall.service.IOrdersService;
import com.hong.taotaomall.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    @Autowired
    private IOrdersService orderService;

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getGoodsId, shoppingCart.getGoodsId());
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartService.getOne(lambdaQueryWrapper);
        shoppingCart.setCreateTime(LocalDateTime.now());
        if (cart != null) {
            if(cart.getGoodsId().equals(shoppingCart.getGoodsId())){
                int number = cart.getNumber() + shoppingCart.getNumber();
                if(number>10){
                    cart.setNumber(10);
                }else {
                    cart.setNumber(number);
                }
                shoppingCartService.updateById(cart);
            }else {
                cart.setNumber(cart.getNumber());
                shoppingCartService.updateById(cart);
            }

        } else {
            Long currentId = BaseContext.getCurrentId();
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
        }
        return Result.success(shoppingCart);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody ShoppingCart shoppingCart) {
//        log.info("购物车数据:{}",shoppingCart);
                shoppingCartService.updateById(shoppingCart);
        return Result.success("购物车修改成功");
    }

    @DeleteMapping
    public Result<String> sub(@RequestParam List<Long> ids) {
        shoppingCartService.deleteByIds(ids);
        return Result.success("宝贝删除成功");
    }

    @GetMapping
    public Result<List<ShoppingCart>> get(@RequestParam List<Long> ids) {
        List<ShoppingCart> list = new ArrayList<>();
        for (Long id : ids) {
            ShoppingCart item = shoppingCartService.getById(id);
            Goods goods = goodsService.getById(item.getGoodsId());
            item.setPrice(goods.getPrice().divide(new BigDecimal(100)));
            list.add(item);
        }
        return Result.success(list);
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long id = BaseContext.getCurrentId();
        lqw.eq(ShoppingCart::getUserId, id);
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        Orders orders = orderService.getById(id);
        List<ShoppingCart> list = null;
        if (orders == null) {
            list = shoppingCartService.list(lqw);
            list = list.stream().map(item ->{
                Goods goods = goodsService.getById(item.getGoodsId());
                item.setPrice(goods.getPrice().divide(new BigDecimal(100)));
                return item;
            }).collect(Collectors.toList());
        } else {
            LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
            ordersWrapper.eq(Orders::getAmount, null);
            list = shoppingCartService.list(lqw);
            list = list.stream().map(item ->{
                Goods goods = goodsService.getById(item.getGoodsId());
                item.setPrice(goods.getPrice().divide(new BigDecimal(100)));
                return item;
            }).collect(Collectors.toList());
        }
        return Result.success(list);
    }

    @DeleteMapping("/clean")
    public Result<String> delete() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long id = BaseContext.getCurrentId();
        lqw.eq(ShoppingCart::getUserId, id);
        shoppingCartService.remove(lqw);
        return Result.success("购物车清空成功");
    }
}
